<template>
  <a-card :bordered="false" class="card-area">
    <div :class="advanced ? 'search' : null">
      <!-- 搜索区域 -->
      <a-form layout="horizontal">
        <a-row>
          <template v-if="advanced">
            <div :class="advanced ? null: 'fold'">
              <a-col :md="12" :sm="24">
                <a-form-item
                  label="收货人"
                  :labelCol="{span: 4}"
                  :wrapperCol="{span: 18, offset: 2}">
                  <a-input v-model="queryParams.mba18" @keyup.enter="search" placeholder="搜索收货人"/>
                </a-form-item>
              </a-col>
              <a-col :md="12" :sm="24">
                <a-form-item
                  label="订单来源"
                  :labelCol="{span: 4}"
                  :wrapperCol="{span: 18, offset: 2}">
                  <a-select v-model="default08" :defaultValue="default08"  @change="handleChange">
                    <a-select-option v-for="person in arr" :key="person">{{person}}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
            </div>
          </template>
          <span style="float: right; margin-top: 3px;">
             <a-button style="margin-left: 8px" @click="reset">重置</a-button>
            <a-button type="primary" @click="search">查询</a-button>
             <a @click="toggleAdvanced" style="margin-left: 8px">
              {{advanced ? '收起' : '展开'}}
              <a-icon :type="advanced ? 'up' : 'down'"/>
            </a>
          </span>
        </a-row>
      </a-form>
    </div>
    <!-- 表格区域 -->
    <a-table ref="TableInfo"
             :columns="columns"
             :dataSource="dataSource"
             :pagination="pagination"
             :defaultExpandAllRows="true"
             :loading="loading"
             :scroll="{ x: 800 }"
             :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
             @change="handleTableChange">
      <template slot="operation" slot-scope="text, record">
        <a-icon type="eye" theme="twoTone" twoToneColor="#4a9ff5" @click="showMbb(record)" title="查看"></a-icon>

        <a-button type="primary" @click="showPrintO(record)">查看电子面单</a-button>
      </template>
    </a-table>
        <!-- 查看模板 -->
        <order-detail
          ref="orderDetail"
          @close="handleOrderDetailClose"
          :orderDetailVisiable="orderDetailVisiable"
          :fileListM="fileListM">
        </order-detail>
        <face-order
          ref="faceOrder"
          @close="handleFaceOrderClose"
          :faceOrderVisiable = "faceOrderVisiable">
        </face-order>
  </a-card>
</template>
<script>
import RangeDate from '@/components/datetime/RangeDate'
import OrderDetail from './components/OrderDetail'
import FaceOrder from './components/FaceOrder'
export default {
  components: {FaceOrder, OrderDetail, RangeDate},
  name: 'Delivered',
  data () {
    return {
      default08: '请选择',
      arr: [],
      orderDetailVisiable: false,
      faceOrderVisiable: false,
      advanced: false,
      fileListM: [],
      mbaFile: {},
      queryParams: {mba09: 2},
      sortedInfo: null,
      filteredInfo: null,
      paginationInfo: null,
      dataSource: [],
      selectedRowKeys: [],
      loading: false,
      pagination: {
        pageSizeOptions: ['10', '20', '30', '40', '100'],
        defaultCurrent: 1,
        defaultPageSize: 10,
        showQuickJumper: true,
        showSizeChanger: true,
        showTotal: (total, range) => `显示 ${range[0]} ~ ${range[1]} 条记录，共 ${total} 条记录`
      }
    }
  },
  computed: {
    columns () {
      let {sortedInfo, filteredInfo} = this
      sortedInfo = sortedInfo || {}
      filteredInfo = filteredInfo || {}
      return [{
        title: '订单ID',
        dataIndex: 'mba00'
      }, {
        title: '订单编号',
        dataIndex: 'mba01'
      }, {
        title: '订单金额',
        dataIndex: 'mba04'
      }, {
        title: '订单来源',
        dataIndex: 'mba08'
      }, {
        title: '订单状态',
        dataIndex: 'mba09',
        key: 'mba09',
        customRender: (text, row, index) => {
          switch (text) {
            case 0:
              return '待付款'
            case 1:
              return '待发货'
            case 2:
              return '已发货'
            case 3:
              return '已完成'
            case 4:
              return '已关闭'
            case 5:
              return '无效订单'
            default:
              return text
          }
        }
        // filters: [
        //   {text: '待付款', value: 0},
        //   {text: '待发货', value: 1},
        //   {text: '已发货', value: 2},
        //   {text: '已完成', value: 3},
        //   {text: '已关闭', value: 4},
        //   {text: '无效订单', value: 5}
        // ],
        // filterMultiple: false,
        // onFilter: (value, record) => record.mba09.includes(value)
      }, {
        title: '收货人',
        dataIndex: 'mba18'
      }, {
        title: '提交时间',
        dataIndex: 'mbacdate',
        sorter: (a, b) => a.mbacdate - b.mbacdate,
        sortOrder: sortedInfo.columnKey === 'mbacdate' && sortedInfo.order
      }, {
        title: '操作',
        dataIndex: 'operation',
        scopedSlots: {customRender: 'operation'},
        fixed: 'right',
        width: 80
      }]
    }
  },
  mounted () {
    this.fetch()
    this.$api.order.getMba().then((r) => {
      let array = r.data.data.records
      console.log(array)
      for (let i = 0; i < array.length; i++) {
        if (array[i].mba09 === 2) {
          this.arr.push(array[i].mba08)
        }
        console.log(this.arr)
      }
    })
  },
  methods: {
    handleChange (value) {
      this.queryParams.mba08 = value
      console.log(value)
    },
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    handleTableChange (pagination, filters, sorter) {
      const pager = { ...this.pagination }
      pager.current = pagination.current
      this.pagination = pager
      this.paginationInfo = pagination
      this.filteredInfo = filters
      this.sortedInfo = sorter
      this.fetch({
        results: pagination.pageSize,
        page: pagination.current,
        sortField: sorter.field,
        sortOrder: sorter.order,
        ...this.queryParams,
        ...filters
      })
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
      if (!this.advanced) {
        this.queryParams.createTimeFrom = ''
        this.queryParams.createTimeTo = ''
      }
    },
    reset () {
      // 取消选中
      this.selectedRowKeys = []
      this.default08 = '请选择'
      // 重置分页
      this.$refs.TableInfo.pagination.current = this.pagination.defaultCurrent
      if (this.paginationInfo) {
        this.paginationInfo.current = this.pagination.defaultCurrent
        this.paginationInfo.pageSize = this.pagination.defaultPageSize
      }
      // 重置列排序规则
      this.sortedInfo = null
      // 重置查询参数
      this.queryParams = {mba09: 2}
      // 清空时间选择
      // this.$refs.createTime.reset()
      this.fetch()
    },
    search () {
      let {sortedInfo, filteredInfo} = this
      let sortField, sortOrder
      // 获取当前列的排序和列的过滤规则
      if (sortedInfo) {
        console.log(sortedInfo)
        sortField = sortedInfo.field
        sortOrder = sortedInfo.order
      }
      this.fetch({
        sortField: sortField,
        sortOrder: sortOrder,
        ...this.queryParams
      })
    },
    showMbb (record) {
      console.log(record)
      this.orderDetailVisiable = true
      let mba00 = record.mba00
      this.$api.order.findMbaInfo(record.mba00).then((r) => {
        console.log(r)
        let datas = r.data.data
        this.$refs.orderDetail.setTableValues(datas)
      })
    },
    handleOrderDetailClose () {
      this.orderDetailVisiable = false
    },
    showPrintO (record) {
      console.log(record)
      let orderCode = record.mba01
      console.log(orderCode)
      this.$refs.faceOrder.setOrderCode(orderCode)
      this.faceOrderVisiable = true
    },
    handleFaceOrderSuccess () {
      this.faceOrderVisiable = false
    },
    handleFaceOrderClose () {
      this.faceOrderVisiable = false
    },
    fetch (params = {mba09: 2}) {
      this.loading = true
      if (this.paginationInfo) {
        // 如果分页信息不为空，则设置表格当前第几页，每页条数，并设置查询分页参数
        this.$refs.TableInfo.pagination.current = this.paginationInfo.current
        this.$refs.TableInfo.pagination.pageSize = this.paginationInfo.pageSize
        params.pageSize = this.paginationInfo.pageSize
        console.log(params.pageSize)
        // 下一页
        params.pageNum = this.paginationInfo.current
        console.log(params.pageNum)
      } else {
        // 如果分页信息为空，则设置为默认值
        params.pageSize = this.pagination.defaultPageSize
        params.pageNum = this.pagination.defaultCurrent
      }
      this.$api.order.getMba({
        ...params
      }).then((r) => {
        console.log(r)
        let data = r.data.data
        this.loading = false
        if (data === null) {
          this.dataSource = []
        } else {
          this.dataSource = data.records
          const pagination = { ...this.pagination }
          console.log(pagination)
          pagination.total = data.total
          this.pagination = pagination
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
  @import "../../../../static/less/Common";
</style>
